/*
 * ImageServer.java
 * 
 * Created on 2007-10-18, 20:28:11
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package info.mikrotubula.server;

import info.mikrotubula.managers.ImagesManagerRemote;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.FileRepresentation;
import org.apache.log4j.Logger;
/**
 *
 * @author Krystian
 */
public class ImageServer extends Restlet{
    static Logger logger = Logger.getLogger(RestServer.class);
    
        @Override
        public void handle(Request request, Response response) {
            logger.info("Image Server starting");
            String imageName = null;
            Integer userId = null;
            try {
                logger.info("Parameters parsing");
                imageName = (String) request.getAttributes().get("imageName");
                userId = Integer.parseInt((String) request.getAttributes().get("userId"));    
                
                Properties props = new Properties();
                props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
                props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
                props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
                props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
                props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
                
                InitialContext ic = new InitialContext(props);
                ImagesManagerRemote imagesManager = (ImagesManagerRemote)ic.lookup(ImagesManagerRemote.class.getName());
                String imageSource = imagesManager.display(userId, imageName);
                
                logger.info("Source lookup");
                FileRepresentation file = new FileRepresentation("C:\\Documents and Settings\\Krystian\\Moje dokumenty\\" + imageSource, MediaType.IMAGE_ALL, 3600);
                response.setEntity(file);
                
            } catch (NamingException ex) {
                ex.printStackTrace();
            } catch (Exception e) {
                logger.error("Something went wrong with parameters");
                logger.error(e);
            }
            
        }
}
